package com.example.mcu_up;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;

import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import com.example.mcu_up.serial.Mcu_Pack;
import com.example.mcu_up.serial.Mcu_Serial;
import com.example.mcu_up.util.Mcu_Msg;

public class Mcu_server extends Service {
	
	
	public boolean b = false;
	Mcu_Serial serialEngine;
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			switch (msg.arg1) {
			case 3:
			Mcu_Pack p = (Mcu_Pack) msg.obj;
		    Toast.makeText(Mcu_server.this,byteToString(p.tobytes()), Toast.LENGTH_SHORT).show();
		    Log.i("mcu_response",byteToString(p.tobytes()));
		    switch (p.getType()) {
			case (byte)0x00:
				
		
				
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_success), Toast.LENGTH_SHORT).show();
				break;
			case (byte)0x02:
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_verify), Toast.LENGTH_SHORT).show();
				break;
			case (byte)0x03:
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_length), Toast.LENGTH_SHORT).show();
				break;
			case (byte)0x04:
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_data), Toast.LENGTH_SHORT).show();
				break;
			case (byte)0x05:
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_cmd), Toast.LENGTH_SHORT).show();
				break;
			case (byte)0x06:
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_device), Toast.LENGTH_SHORT).show();
				break;
			case (byte)0x07:
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_version), Toast.LENGTH_SHORT).show();
				break;
			case (byte)0x08:
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_checksum), Toast.LENGTH_SHORT).show();
				break;
			case (byte)0x09:
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_array), Toast.LENGTH_SHORT).show();
				break;
			case (byte)0x0A:
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_row), Toast.LENGTH_SHORT).show();
				break;
			case (byte)0x0C:
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_app), Toast.LENGTH_SHORT).show();
				break;
			case (byte)0x0D:
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_active), Toast.LENGTH_SHORT).show();
				break;
			case (byte)0x0F:
				 Toast.makeText(Mcu_server.this,Mcu_server.this.getString(R.string.s_unk), Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
			break;
			default:
				break;
			}
		};
	};
	@SuppressLint("DefaultLocale")
	public static String byteToString(byte[] b) {
		StringBuffer buffer = new StringBuffer(b.length);
		for (int i = 0; i < b.length; i++) {
			String strHex = Integer.toHexString(b[i]).toUpperCase();

			if (strHex.length() > 3) {
				buffer.append(strHex.substring(6) + " ");
			} else if (strHex.length() < 2) {
				buffer.append("0" + strHex + " ");
			} else {
				buffer.append(strHex + " ");
			}
		}
		return buffer.toString();
	}

	public IBinder onBind(Intent arg0) {
		
		return null;
	}
	
	@Override
	public void onCreate() {
		
		serialEngine=	Mcu_Serial.getInstance();
		serialEngine.setHandler(handler);
	    serialEngine.start();
	}
	
}
